{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Scala API to EasyForm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "val form : EasyForm = new com.twosigma.beakerx.scala.easyform.EasyForm(\"Form and Run\")\n",
    "form.addTextField(\"first\")\n",
    "form.put(\"first\", \"First\")\n",
    "form.addTextField(\"last\")\n",
    "form.put(\"last\", \"Last\")\n",
    "form.addButton(\"Go!\", \"run\")\n",
    "\n",
    "form"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can access the values from the form with get method:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "run"
    ]
   },
   "outputs": [],
   "source": [
    "\"Good morning \" + form.get(\"first\") + \" \" + form.get(\"last\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can set default values on the fields with put method:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "form.put(\"first\", \"Beaker\")\n",
    "form.put(\"last\", \"Berzelius\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Event Handlers for Smarter Forms\n",
    "\n",
    "You can use `onInit` and `onChange` to handle component events. For button events use `actionPerformed` or `addAction`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "//You can use onInit and onChange to handle component events. For button events use actionPerfromed or addAction.\n",
    "val f1 = new EasyForm(\"Form and Run\")\n",
    "val first = f1.addTextField(\"first\", 15)\n",
    "val last = f1.addTextField(\"last\", 15).onInit(new EasyFormListener {\n",
    "      override def execute(value: String): Unit = {\n",
    "        f1.put(\"last\", \"setinit1\");\n",
    "      }\n",
    "    }).onChange(new EasyFormListener {\n",
    "      override def execute(value: String): Unit = {\n",
    "        first.setValue(f1.get(\"last\") + \" extra\");\n",
    "      }\n",
    "    });\n",
    "val button = f1.addButton(\"action\", \"action_button\")\n",
    "button.actionPerformed = new EasyFormListener {\n",
    "      override def execute(value: String): Unit = f1.put(\"last\", \"action done\");\n",
    "}\n",
    "\n",
    "f1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "action_button"
    ]
   },
   "outputs": [],
   "source": [
    "f1.get(\"last\") + \", \" + f1.get(\"first\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f1.put(\"last\", \"new Value\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "f1.put(\"first\", \"new Value2\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## All Kinds of Fields"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "val g = new EasyForm(\"Field Types\")\n",
    "g.addTextField(\"Short Text Field\", 10)\n",
    "g.addTextField(\"Text Field\")\n",
    "g.addPasswordField(\"Password Field\", 10)\n",
    "g.addTextArea(\"Text Area\")\n",
    "g.addTextArea(\"Tall Text Area\", 10, 5)\n",
    "g.addCheckBox(\"Check Box\")\n",
    "val options = Seq(\"a\", \"b\", \"c\", \"d\")\n",
    "g.addComboBox(\"Combo Box\", options)\n",
    "g.addComboBox(\"Editable Combo\", options, true)\n",
    "\n",
    "g.addList(\"List\", options)\n",
    "g.addList(\"List Single\", options, false)\n",
    "g.addList(\"List Two Row\", options, 2)\n",
    "\n",
    "g.addCheckBoxes(\"Check Boxes\", options)\n",
    "g.addCheckBoxes(\"Check Boxes H\", options, EasyForm.HORIZONTAL)\n",
    "\n",
    "g.addRadioButtons(\"Radio Buttons\", options)\n",
    "g.addRadioButtons(\"Radio Buttons H\", options, EasyForm.HORIZONTAL)\n",
    "\n",
    "g.addDatePicker(\"Date\")\n",
    "\n",
    "g.addButton(\"Go!\", \"run2\")\n",
    "g"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "run2"
    ]
   },
   "outputs": [],
   "source": [
    "import java.util.HashMap\n",
    "import java.util.function.Consumer\n",
    "\n",
    "val result = new HashMap[String, Object]()\n",
    "g.keySet().forEach(\n",
    "new Consumer[String] {\n",
    "    override def accept(key: String): Unit = {\n",
    "      result.put(key, g.get(key))\n",
    "    }\n",
    "  }\n",
    ")\n",
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Dates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "val gdp = new EasyForm(\"Field Types\")\n",
    "val date = gdp.addDatePicker(\"Date\")\n",
    "gdp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "gdp.get(\"Date\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### SetDate "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import java.util.Date\n",
    "\n",
    "val easyForm = new EasyForm(\"Field Types\")\n",
    "// Setup date via String (format is yyyyMMdd) also works\n",
    "easyForm.addDatePicker(\"Date\").setDate(new Date())\n",
    "easyForm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Default Values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "val h = new EasyForm(\"Default Values\")\n",
    "h.addTextArea(\"Default Value\", \"Initial value\")\n",
    "h.addCheckBox(\"Default Checked\", true)\n",
    "h.addButton(\"Press\", \"check\")\n",
    "h"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "check"
    ]
   },
   "outputs": [],
   "source": [
    "val result = new HashMap[String, Object]()\n",
    "h.keySet().forEach(\n",
    "new Consumer[String] {\n",
    "    override def accept(key: String): Unit = {\n",
    "      result.put(key, h.get(key))\n",
    "    }\n",
    "  }\n",
    ")\n",
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## JupyterJSWidgets work with EasyForm\n",
    "\n",
    "The widgets from [JupyterJSWidgets](JavaWidgets.ipynb) are compatible and can appear in forms."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import com.twosigma.beakerx.widget.IntSlider\n",
    "\n",
    "val w = new IntSlider()\n",
    "\n",
    "val widgetForm = new EasyForm(\"pyhton widgets\")\n",
    "widgetForm.addButton(\"Press\", \"widget_test\")\n",
    "widgetForm.addWidget(\"IntSlider\",w)\n",
    "widgetForm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "tags": [
     "widget_test"
    ]
   },
   "outputs": [],
   "source": [
    "widgetForm.get(\"IntSlider\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "beakerx_kernel_parameters": {},
  "celltoolbar": "Tags",
  "kernelspec": {
   "display_name": "Scala",
   "language": "scala",
   "name": "scala"
  },
  "language_info": {
   "codemirror_mode": "text/x-scala",
   "file_extension": ".scala",
   "mimetype": "",
   "name": "Scala",
   "nbconverter_exporter": "",
   "version": "2.11.12"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": false,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": false,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
